package MyBlog.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import MyBlog.biz.ArticleBiz;
import MyBlog.biz.CategoryBiz;
import MyBlog.biz.UserBiz;
import MyBlog.entity.Article;
import MyBlog.entity.Category;
import MyBlog.entity.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Component("articleAction")
public class ArticleAction extends ActionSupport {

	public void setState(String state) {
		this.state = state;
	}

	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public String getTitle() {
		return title;
	}

	public int getCid() {
		return cid;
	}

	public List<Article> getArticleList() {
		return articleList;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public User getUser() {
		return user;
	}

	public int getArticleId() {
		return articleId;
	}

	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}

	// 数组
	private List<Article> articleUpdate;
	private List<Article> articleClicks;
	private List<Article> articleList;
	private List<User> hotUser;
	private List<Category> categoryList;
	private List<Integer> page = new ArrayList<Integer>();

	public List<Integer> getPage() {
		return page;
	}

	public List<Article> getArticleUpdate() {
		return articleUpdate;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public List<Article> getArticleClicks() {
		return articleClicks;
	}

	public List<User> getHotUser() {
		return hotUser;
	}

	// 实现类
	@Autowired
	private ArticleBiz articleBiz;
	@Autowired
	private UserBiz userBiz;
	@Autowired
	private CategoryBiz categoryBiz;

	// 数据
	private int userId;
	private User blogUser;
	private Article article;
	private User user;
	private int pageNum;
	private int totalPage;
	private int articleId;
	private int cid;
	private String state;
	private String title;
	private String check;

	public User getBlogUser() {
		return blogUser;
	}

	public int getUserId() {
		return userId;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	// 方法

	// 首页
	public String index() {
		articleClicks = articleBiz.getListByClicks(5);
		articleUpdate = articleBiz.getListByUpdateTime(10);
		hotUser = userBiz.getUserByVisitTimes(10);
		return SUCCESS;
	}

	public String sidebar() {
		blogUser = userBiz.fetchUserById(userId);
		categoryList = categoryBiz.getListByUserId(userId);
		articleClicks = articleBiz.getClicksListByUserId(userId, 8);
		return SUCCESS;
	}

	// 博主主页
	public String blogPage() {
		user = userBiz.fetchUserById(userId);
		int pageSize = 3;
		System.out.println(articleId);
		if (pageNum < 1)
			pageNum = 1;
		int rows = articleBiz.getRowsByUserId(userId,cid);
		totalPage = rows % pageSize == 0 ? rows / pageSize : rows / pageSize
				+ 1;
		for (int i = 0; i < totalPage; i++) {
			page.add(i + 1);
		}
		user.setVisitTimes(user.getVisitTimes()+1);
		userBiz.update(user);
		articleUpdate = articleBiz.getListByUserIdAndCid(cid,userId, pageNum, pageSize);
		return SUCCESS;
	}

	// 头部
	public String header() {
		blogUser = userBiz.fetchUserById(userId);
		return SUCCESS;
	}

	// 博文详细
	public String articleBlog() {
		blogUser = userBiz.fetchUserById(userId);
		categoryList = categoryBiz.getListByUserId(userId);
		articleClicks = articleBiz.getClicksListByUserId(userId, 8);
		return SUCCESS;
	}

	// 博文管理
	public String manage() {
		Map<String, Object> sess = ActionContext.getContext().getSession();
		User user = (User) sess.get("user");
		categoryList = categoryBiz.getListByUserId(user.getId());
		articleList = articleBiz.getListByCidAndtitle(cid, title, user.getId());
		return SUCCESS;
	}

	// 编辑页面
	public String editPage() {
		HttpSession sess = ServletActionContext.getRequest().getSession();
		User users = (User) sess.getAttribute("user");
		categoryList = categoryBiz.getListByUserId(users.getId());
		if(articleId>0){
			article = articleBiz.fetchArticleById(articleId);
			if (article.getState() > 0)
				check = "checked";
			else
				check = "";
		}
		return SUCCESS;
	}

	// 编辑文章
	public String edit() {
		Category c = categoryBiz.ftechById(cid);
		article.setState(state==null?0:1);
		User users = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		Date time = new Date(System.currentTimeMillis());
		if (articleId > 0) {
			Article articleUpdate = articleBiz.fetchArticleById(articleId);
			articleUpdate.setTitle(article.getTitle());
			articleUpdate.setContent(article.getContent());
			articleUpdate.setCategory(c);
			articleUpdate.setUpdateTime(time);
			articleUpdate.setState(article.getState());
			articleBiz.update(articleUpdate);
		} else {
			article.setUser(users);
			article.setUpdateTime(time);
			article.setCreateTime(time);
			articleBiz.add(article);
		}
		return SUCCESS;
	}
	
	public String delete(){
		articleBiz.delete(articleId);
		return SUCCESS;
	}

}
